package org.example.restful.Controller;

import org.example.restful.bean.Employee;
import org.example.restful.common.R;
import org.example.restful.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/v1")
public class EmployeeRestController {

    @Autowired
    EmployeeService employeeService;




//    @GetMapping(value = "/{id}")
//    public String getEmployee(@PathVariable Long id) {
//        Employee emp = employeeService.getEmp(id);
//        return "get" + emp;
//    }
    @GetMapping(value = "/employee/{id}")
    public R<Employee> getEmployee(@PathVariable Long id) {
        Employee emp = employeeService.getEmp(id);
        return R.success(emp);
    }



//    @DeleteMapping(value = "/{id}")
//    public String deleteEmployee(@PathVariable Long id) {
//        employeeService.deleteEmp(id);
//        return "delete" + id;
//    }

    @DeleteMapping(value = "/employee/{id}")
    public R deleteEmployee(@PathVariable Long id) {
        employeeService.deleteEmp(id);
        return R.success();
    }

//    @PostMapping()
//    public String saveEmployee(@RequestBody Employee employee) {
//        employeeService.saveEmp(employee);
//        return "save" + employee;
//    }
    @PostMapping("/employee")
    public R saveEmployee(@RequestBody Employee employee) {
        employeeService.saveEmp(employee);
        return R.success();
    }

//    @PutMapping()
//    public String updateEmployee(@RequestBody Employee employee) {
//        employeeService.updateEmp(employee);
//        return "update" + employee;
//    }

    @PutMapping("/employee")
    public R updateEmployee(@RequestBody Employee employee) {
        employeeService.updateEmp(employee);
        return R.success();
    }

    @GetMapping("/employees")
    public R<List<Employee>> getEmployees() {
        return R.success(employeeService.getEmployees());
    }


}
